﻿/*
 * 2012  - C.B.S. - Compute Software Solutions
 * This file is part of Observation Log.
 * 
 *     Observation Log is free software: you can redistribute it and/or modify
 *     it under the terms of the GNU General Public License as published by
 *     the Free Software Foundation, either version 3 of the License, or
 *     (at your option) any later version.
 * 
 *     Observation Log is distributed in the hope that it will be useful,
 *     but WITHOUT ANY WARRANTY; without even the implied warranty of
 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *     GNU General Public License for more details.
 * 
 *     You should have received a copy of the GNU General Public License
 *     along with Observation Log.  If not, see <http://www.gnu.org/licenses/>.
 */
using System;
using System.Data;
using System.Windows.Forms;

namespace ObservationLog
{
    public partial class frmMain : Form
    {
        public frmMain()
        {
            InitializeComponent();
        }

        #region EventHandlers
        private void frmObjects_Load(object sender, EventArgs e)
        {
            this.instrumentsViewTableAdapter.Fill(this.observationLogDBDataSet.InstrumentsView);
            this.observeLogTableAdapter.Fill(this.observationLogDBDataSet.ObserveLog);
            this.objectsTableAdapter.Fill(this.observationLogDBDataSet.Objects);

            cmbSort.SelectedIndex = 1;
            cmbFilter.SelectedIndex = 0;
        }

        private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            using (frmAboutBox a = new frmAboutBox())
            {
                a.ShowDialog(this);
            }
        }

        private void cmbSort_SelectedIndexChanged(object sender, EventArgs e)
        {
            switch (cmbSort.SelectedIndex)
            {
                case 0: //By object name        
                    this.objectsBindingSource.Sort = "ItemDesc";
                    break;
                case 1: //By Messier number
                    this.objectsBindingSource.Sort = "MessierNumber, NGC";
                    break;
                case 2: //By marathon number
                    this.objectsBindingSource.Sort = "ItemId";
                    break;
            }
        }

        private void cmbFilter_SelectedIndexChanged(object sender, EventArgs e)
        {
            switch (cmbFilter.SelectedIndex)
            {
                case 0: //All objects            
                    this.objectsBindingSource.Filter = "";
                    break;
                case 1: //Only observed objects
                    this.objectsBindingSource.Filter = "NumberObservationRecords > 0";
                    break;
                case 2: //Only non-observed objects
                    this.objectsBindingSource.Filter = "NumberObservationRecords = 0";
                    break;
                case 3: //Objects observed a single time
                    this.objectsBindingSource.Filter = "NumberObservationRecords = 1";
                    break;
                case 4: //Objects observed multiple times
                    this.objectsBindingSource.Filter = "NumberObservationRecords > 1";
                    break;
            }
        }

        private void objectsBindingSource_CurrentItemChanged(object sender, EventArgs e)
        {
            SaveChanges();
        }

        private void frmObjects_FormClosing(object sender, FormClosingEventArgs e)
        {
            SaveChanges();
        }

        private void objectsBindingSource_CurrentChanged(object sender, EventArgs e)
        {
            ShowImage();
        }

        private void pictureBox1_Click(object sender, EventArgs e)
        {
            DataRowView drv = ((DataRowView)this.objectsBindingSource.Current);
            if (drv != null)
            {
                frmViewChart vc = new frmViewChart(drv["ItemName"].ToString(),
                    Convert.ToInt32(drv["RAHour"]),
                     Convert.ToSingle(drv["RAMinute"]),
                     Convert.ToInt32(drv["DecDeg"]),
                     Convert.ToSingle(drv["DecMinute"]));
                vc.Show();
            }
        }

        private void lbEditList_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            using (frmObjectList f = new frmObjectList())
            {
                f.ShowDialog(this);
                if (f.DialogResult == System.Windows.Forms.DialogResult.OK)
                {
                    //May be changed data, new items etc..
                    RefreshList();
                }
            }
        }

        private void dgvObserveLog_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            //If edit column was clicked...
            if (dgvObserveLog.Columns[e.ColumnIndex].Name == "EditColumn") 
            {
                DataRowView drv = ((DataRowView)this.objectsBindingSource.Current);
                using (frmObserveLog frmObLog = new frmObserveLog())
                {
                    var observeLogRow = (DataRowView)dgvObserveLog.Rows[e.RowIndex].DataBoundItem;
                    frmObLog.observeLogBindingSource.Filter = "ObserveLogId=" +
                        (observeLogRow)["ObserveLogId"].ToString();
                    frmObLog.ItemName = drv["ItemName"].ToString();
                    frmObLog.ItemDesc = Convert.ToString(drv["ItemDesc"]);
                    frmObLog.ItemType = Convert.ToString(drv["ItemTypeName"]);
                    frmObLog.ItemId = Convert.ToInt32(drv["ItemId"]);
                    if (frmObLog.ShowDialog(this) == DialogResult.OK)
                    {
                        //Update list - things may have changed - such as the times observed, filter results etc...
                        RefreshList();
                    }
                }
            }
        }

        private void llAddNewObservation_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            DataRowView drv = ((DataRowView)this.objectsBindingSource.Current);
            using (frmObserveLog frmObLog = new frmObserveLog())
            {
                frmObLog.observeLogBindingSource.Filter = "ItemID=" + drv["ItemId"];
                frmObLog.ItemName = drv["ItemName"].ToString();
                frmObLog.ItemDesc = Convert.ToString(drv["ItemDesc"]);
                frmObLog.ItemType = Convert.ToString(drv["ItemTypeName"]);
                frmObLog.ItemId = Convert.ToInt32(drv["ItemId"]);
                frmObLog.AddingNew = true;
                if (frmObLog.ShowDialog(this) == DialogResult.OK)
                {
                    //Update list - things may have changed - such as the times observed, filter results etc...
                    RefreshList(); 
                }
            }
        }

        private void frmMain_Resize(object sender, EventArgs e)
        {
            ShowImage();
        }

        private void splitContainer2_SplitterMoved(object sender, SplitterEventArgs e)
        {
            ShowImage();
        }
        #endregion

        #region Private Functions
        private void RefreshList()
        {
            int position = objectsBindingSource.Position;
            this.objectsTableAdapter.Fill(this.observationLogDBDataSet.Objects);
            this.observeLogTableAdapter.Fill(this.observationLogDBDataSet.ObserveLog);
            objectsBindingSource.Position = position;
        }

        private void SaveChanges()
        {
            this.Validate();
            this.objectsBindingSource.EndEdit();
            this.tableAdapterManager.UpdateAll(this.observationLogDBDataSet);
        }

        private void ShowImage()
        {
            DataRowView drv = ((DataRowView)this.objectsBindingSource.Current);
            if (drv != null)
            {
                this.pictureBox1.Image = Program.GetChartImage(this.pictureBox1.Width,
                    this.pictureBox1.Height,
                    Convert.ToInt32(drv["RAHour"]),
                    Convert.ToSingle(drv["RAMinute"]),
                    Convert.ToInt32(drv["DecDeg"]),
                    Convert.ToSingle(drv["DecMinute"]));
                this.pictureBox1.Refresh();
            }
            else
            {
                this.pictureBox1.Image = null;
            }
        }
        #endregion                
    }
}